package com.wl.center.state.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.center.common.result.Result;
import com.wl.center.model.state.Customer;
import com.wl.center.model.state.UpdateContent;
import com.wl.center.state.service.CustomerService;
import com.wl.center.state.service.UpdateContentService;
import com.wl.center.state.service.UserService;
import com.wl.center.vo.state.UpdateRecord;
import com.wl.center.vo.task.CustomerInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags="状态修改")
@RequestMapping("/modify")
public class StateController {
    //注入service
    @Autowired
    private CustomerService customerService;
    @Autowired
    private UpdateContentService updateContentService;
    @Autowired
    private UserService userService;
    //1.点击写跟进后确定，提交写跟进内容
    @ApiOperation(value = "写跟进内容")
    @PostMapping("/follow-up/{sellerId}/{customerId}/{content}")
    public Result postFollowUp(@PathVariable String content,@PathVariable Integer sellerId,@PathVariable Integer customerId) {
        UpdateContent updateContent = new UpdateContent();
        updateContent.setContent(content);
        updateContent.setUpdatePerson(sellerId);
        updateContent.setCustomerId(customerId);
        boolean flag =  updateContentService.save(updateContent);//这里注意mp添加数据时，它的自增id会变得很长

        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value="导出跟进内容")
    @GetMapping("/exportUpdateContent")
    public void exportUpdateContent(HttpServletResponse response){
        customerService.exportUpdateContent(response);
    }

    //3.分页查询跟进信息
    @ApiOperation(value = "分页查询跟进信息")
    @GetMapping("/get/{currentPage}/{rows}/{CustomerId}")
    public Result getNew(@PathVariable long currentPage,
                         @PathVariable long rows,
                         @PathVariable long CustomerId
    ) {
        //创建page对象，传递当前页，每页记录数
        Page<UpdateContent> page = new Page<>(currentPage,rows);
        //构建条件，用条件构造器
        QueryWrapper<UpdateContent> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(CustomerId)) {
            wrapper.eq("customer_id",CustomerId);
        }
        wrapper.orderByAsc("update_time");//将查询到的数据按创建时间的由早到晚排列，升序
        IPage<UpdateRecord> page1 = updateContentService.getUpdateContentPage(page, wrapper);
        //返回结果
        return Result.ok(page1);
    }
}
